from ast import TypeAlias
import pygame as pg
import os

# 字体路径
TTF_PATH = os.path.join(os.path.dirname(__file__), r'game_file\simhei.ttf')

# 渲染带描边的文本
def render_outlined_text(font, text, text_color, outline_color, outline_width=2):
    # 渲染描边
    outlines = []
    for dx in range(-outline_width, outline_width+1):
        for dy in range(-outline_width, outline_width+1):
            if dx != 0 or dy != 0:
                outline = font.render(text, True, outline_color)
                outlines.append((outline, (dx, dy)))
    # 渲染中心文本
    main_text = font.render(text, True, text_color)
    # 计算最终表面大小
    width = main_text.get_width() + outline_width * 2
    height = main_text.get_height() + outline_width * 2
    surface = pg.Surface((width, height), pg.SRCALPHA)
    # 绘制
    for outline, offset in outlines:
        surface.blit(outline, (outline_width + offset[0], outline_width + offset[1]))
    surface.blit(main_text, (outline_width, outline_width))
    return surface

class Text_BW: # 描边的文本 特殊文本格式生成器
    def __init__(self, text:str, font_size:int, text_color, outline_color, outline_width=2,font_path=TTF_PATH):
        self.font = pg.font.Font(font_path, font_size)
        self.text_surface = render_outlined_text(self.font, text, text_color, outline_color, outline_width)

    def draw(self, win, position:tuple[int,int]):
        win.blit(self.text_surface, position)
        
class Text: # 普通文本生成器
    def __init__(self, text:str, font_size:int, text_color,font_path=TTF_PATH):
        self.font = pg.font.Font(font_path, font_size)
        self.text_surface = self.font.render(text, True, text_color)

    def draw(self, win, position:tuple[int,int]):
        win.blit(self.text_surface, position)
        

